package com.wangjiyuan.shiwupai.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.GlideBitmapDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.wangjiyuan.shiwupai.R;
import com.wangjiyuan.shiwupai.base.BaseApplication;
import com.wangjiyuan.shiwupai.bean.guangchi.evaluation.FeedsBean;
import com.wangjiyuan.shiwupai.ui.activity.detail.DetailActivity;
import com.wangjiyuan.shiwupai.utils.RecyclerViewAdapterHelper;

import java.util.List;

/**
 * Created by wjy on 2016/11/30.
 */

public class EvaluationAdapter extends RecyclerViewAdapterHelper<FeedsBean> {

    public EvaluationAdapter(Context context, List list) {
        super(context, list);
    }

    @Override
    public RecyclerView.ViewHolder onCreateMyViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.item_evaluation_4, parent, false);
        return new ViewHolder_4(view);
    }

    @Override
    public void onBindMyViewHolder(RecyclerView.ViewHolder holder, int position) {
        ((ViewHolder_4) holder).newInstance();
    }

    private class ViewHolder_4 extends RecyclerView.ViewHolder {
        private CardView item_evaluation_4;
        private TextView tail, title, source;

        public ViewHolder_4(View itemView) {
            super(itemView);
            item_evaluation_4 = (CardView) itemView.findViewById(R.id.item_evaluation_4);
            tail = (TextView) itemView.findViewById(R.id.tail);
            title = (TextView) itemView.findViewById(R.id.title);
            source = (TextView) itemView.findViewById(R.id.source);
        }

        public void newInstance() {
            final FeedsBean feedsBean = mList.get(getAdapterPosition());
            tail.setText(feedsBean.getTail() + "");
            title.setText(feedsBean.getTitle() + "");
            source.setText(feedsBean.getSource() + "");
            ViewGroup.LayoutParams layoutParams = item_evaluation_4.getLayoutParams();
            layoutParams.height = BaseApplication.width * 430 / 690;
            Glide.with(mContext).
                    load(feedsBean.getBackground())
                    .asBitmap().into(new SimpleTarget<Bitmap>() {
                @Override
                public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                    item_evaluation_4.setBackground(new GlideBitmapDrawable(mContext.getResources(), resource));
                }
            });
            item_evaluation_4.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(mContext, DetailActivity.class);
                    intent.putExtra("link", feedsBean.getLink());
                    mContext.startActivity(intent);
                }
            });
        }
    }
}
